<?php

namespace app\common\model;

use think\Model;


class OrderShipping extends Model
{

    

    

    // 表名
    protected $name = 'order_shipping';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'create_time_text',
        'update_time_text'
    ];
	
	
	
	public function shipping()
	{
		return $this->belongsTo('shipping', 'shipping_id', 'id', '', 'left')->setEagerlyType(0);
	}



    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }
	
	public function getListByOrderId($orderId)
	{
		return $this->alias('os')
			->join('shipping s', 'os.shipping_id = s.id', 'left')
			->where(['os.order_id' => $orderId])
			->order(['os.create_time' => 'asc'])
			->field('s.name, s.code, os.shipping_no')
			->select();
	}
	
	protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
